package io.renren.modules.app.controller;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;

import cn.hutool.core.util.EscapeUtil;
import io.renren.common.utils.R;
import io.renren.modules.app.entity.ibps.BpmDef;
import io.renren.modules.app.entity.nosql.LcPdfTemplate;
import io.renren.modules.app.nosql.mongo.ILcPdfTemplate;
import io.renren.modules.app.service.IBpmDefService;
import io.renren.modules.job.entity.ScheduleJobEntity;
import io.renren.modules.job.service.ScheduleJobService;
import jodd.io.FileUtil;

/**
 * 流程定义
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/app/lc")
public class LcTemplateController {
	@Autowired
	IBpmDefService bpmDefService;
	@Autowired
	ScheduleJobService scheduleJobService;
	@Autowired
	ILcPdfTemplate templateService;
	
	public static void main(String[] args) throws IOException {
		String html = FileUtil.readString(new File("d:/json.json"));
		System.out.println(html);
		System.out.println(HtmlUtils.htmlUnescape(html));
//		System.out.println(EscapeUtil.safeUnescape(html));
//		System.out.println(StringEscapeUtils.escapeHtml(html));
	}

	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) {
		LcPdfTemplate bpmDef = new LcPdfTemplate().setIdBpmDef(id);
		Optional<LcPdfTemplate> findById = templateService.findOne(Example.of(bpmDef));

//		Optional<LcPdfTemplate> findById = templateService.findById(lcId);
		if (!findById.isPresent()) {
			return R.error("模板id为找不到");
		}
		LcPdfTemplate lcPdfTemplate = findById.get();
		lcPdfTemplate.setHtml(lcPdfTemplate.getHtml());
		return R.ok().put("lcTemplate", lcPdfTemplate);
	}

	@PostMapping("/save")
	public R save(@RequestParam Map<String, Object> params) {
		Optional<Object> idOpt = Optional.of(params.get("id"));
		Optional<Object> htmlOpt = Optional.of(params.get("html"));
		if (!htmlOpt.isPresent()) {
			return R.error("html内容为null");
		}
		if (!idOpt.isPresent()) {
			return R.error("流程id为null");
		}
		Optional<BpmDef> bpmDefOpt = bpmDefService.findById(idOpt.get().toString());
		if (!bpmDefOpt.isPresent()) {
			return R.error("查询不到流程定义:" + idOpt.get());
		}
		BpmDef bpmDef = bpmDefOpt.get();
		// 找出关联表
		LcPdfTemplate lcPdfTemplate = null;
		Optional<LcPdfTemplate> findById = templateService.findOne(Example.of(new LcPdfTemplate().setIdBpmDef(bpmDef.getId())));
		if (findById.isPresent()) {
			lcPdfTemplate = findById.get();
		} else {
			// 不存在则创建
			lcPdfTemplate = new LcPdfTemplate(bpmDef);
		}
		String html = htmlOpt.get().toString();
		lcPdfTemplate.setHtml(html);
		LcPdfTemplate save = templateService.save(lcPdfTemplate);
		if (save != null) {
			return R.ok("保存成功");
		}
		return R.error("保存失败");
	}

	@GetMapping("/list")
	public R list() {
		ScheduleJobEntity job = scheduleJobService.selectById("2");
	
		String params = job.getParams();
		System.out.println(params);
		List<BpmDef> findBpmDefByKyes = bpmDefService.findBpmDefByKyes(Arrays.asList(params.split(",")));

		return R.ok().put("list", findBpmDefByKyes);
	}
}
